﻿using System;
using System.Collections.Generic;
using System.Windows;
using mal.WP7.GoogleAnalytics.Business.Repository;

namespace mal.WP7.GoogleAnalytics.Business.GoogleApi
{
    public class GoogleAnalyticsServiceMoke : GoogleAnalyticsServiceBase
    {
        public GoogleAnalyticsServiceMoke(string login, string password)
            : base(login, password)
        {

        }

        public override void LoginAsync(object useState)
        {
            Deployment.Current.Dispatcher.BeginInvoke(() =>
            {
                List<Website> websites = new List<Website>();

                for (int i = 0; i < 4; i++)
                {
                    Website website = new Website();
                    website.Id = string.Format("{1} - website {0}", i, this.Login);
                    website.Name = string.Format("{1} - website {0}", i, this.Login);
                    websites.Add(website);
                }

                LoginCompletedEventArgs args = new LoginCompletedEventArgs(websites, useState);
                this.RaiseLoginCompletedEvent(args);
            });
        }

        public override void GetFeedAsync(FeedRequest request, object userState)
        {
            Deployment.Current.Dispatcher.BeginInvoke(() =>
            {
                Random rnd = new Random();

                GetFeedCompletedEventArgs args = new GetFeedCompletedEventArgs(userState);
                List<Statistic> values = new List<Statistic>();

                TimeSpan duration = request.EndDate - request.StartDate;

                foreach (Metric metric in request.Metrics)
                {
                    DateTime date = request.StartDate;
                    for (int i = 0; i <= duration.TotalDays; i++)
                    {
                        Statistic stat = new Statistic();
                        stat.Dimension = request.Dimension;
                        stat.Metric = metric;
                        stat.StartDate = date;

                        switch (request.Dimension)
                        {
                            case Dimension.Day:
                                stat.Value = rnd.Next(0, 200);
                                stat.EndDate = date.AddDays(1);
                                break;
                            case Dimension.Week:
                                stat.Value = rnd.Next(0, 1400);
                                stat.EndDate = date.AddDays(7);
                                break;
                            case Dimension.Month:
                                stat.Value = rnd.Next(0, 5000);
                                stat.EndDate = date.AddDays(30);
                                break;
                            case Dimension.Year:
                                stat.Value = rnd.Next(0, 500000);
                                stat.EndDate = date.AddDays(365);
                                break;
                            default:
                                stat.Value = rnd.Next(0, 200);
                                break;
                        }

                        values.Add(stat);

                        date = stat.EndDate;
                    }
                }

                args.WebsiteId = request.WebSiteId;
                args.Statistics = values;
                this.RaiseGetFeedCompletedEvent(args);
            });
        }
    }
}
